package com.gzit.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.gzit.R;

/**
 * 在viewflip界面切换时，显示当前界面的指示[。。.。。]
 * 
 * <com.gzit.view.FlipIndicatorView <br>
 * android:layout_width="100dp"<br>
 * android:layout_height="30dp"<br>
 * gzit:indicator_number="3"<br>
 * android:paddingLeft="5dp"<br>
 * android:paddingRight="5dp"<br>
 * gzit:radius="5dp" ><br>
 * </com.gzit.view.FlipIndicatorView><br>
 * 
 * @author teddy
 * 
 */
public class FlipIndicatorView extends View {

	private final int CUR_COLOR = Color.WHITE;
	private final int NORMAL_COLOR = Color.parseColor("#e4e4e4");

	int curColor = CUR_COLOR;
	int normalColor = NORMAL_COLOR;

	int bgColor = Color.parseColor("#B2cfcfcf");

	private boolean drawBg = true;

	private int indicatorNumber; // 总共有几个面板
	private int currentIndicator;// 从0开始数

	private float radius = 10;

	public FlipIndicatorView(Context context) {
		super(context);
	}

	public FlipIndicatorView(Context context, AttributeSet attrs) {
		super(context, attrs);

		TypedArray arr = context.obtainStyledAttributes(attrs,
				R.styleable.FlipIndicatorView);
		radius = arr.getDimension(R.styleable.FlipIndicatorView_radius, 10);
		indicatorNumber = arr.getInt(
				R.styleable.FlipIndicatorView_indicator_number, 1);

		this.curColor = arr.getColor(
				R.styleable.FlipIndicatorView_normal_color, CUR_COLOR);
		this.normalColor = arr.getColor(
				R.styleable.FlipIndicatorView_sel_color, NORMAL_COLOR);

		this.drawBg = arr.getBoolean(R.styleable.FlipIndicatorView_draw_bg,
				true);

		arr.recycle();

	}

	public void setIndicatorNumber(int indicatorNumber) {

		this.indicatorNumber = indicatorNumber;
		this.invalidate();
	}

	public void showNext() {
		currentIndicator++;
		if (currentIndicator == indicatorNumber) {
			currentIndicator = 0;
		}

		this.invalidate();

	}

	public void showPrev() {
		currentIndicator--;
		if (currentIndicator == -1) {
			currentIndicator = indicatorNumber - 1;
		}
		this.invalidate();

	}

	private void drawBg(Canvas canvas) {

		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(bgColor);
		canvas.drawRoundRect(
				new RectF(0, 0, this.getWidth(), this.getHeight()),
				this.getHeight() / 2, this.getHeight() / 2, paint);

	}

	private void drawIndicator(Canvas canvas) {

		Paint paint = new Paint();
		paint.setAntiAlias(true);

		int width = this.getWidth();
		int height = this.getHeight();

		int w = width - this.getPaddingLeft() - this.getPaddingRight();

		float widthStep = (float) w / (this.indicatorNumber + 1);
		float heightStep = (float) height / 2;

		for (int i = 0; i < indicatorNumber; i++) {
			if (i == currentIndicator) {
				paint.setColor(curColor);
			} else {
				paint.setColor(normalColor);
			}

			canvas.drawCircle(i * widthStep + widthStep, heightStep, radius,
					paint);

		}

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if (this.drawBg) {
			this.drawBg(canvas);
		}
		this.drawIndicator(canvas);

	}

	public int getIndicatorNumber() {
		return indicatorNumber;
	}

}
